package com.example.aquanliangblogscan.util;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.example.aquanliangblogscan.entity.Article;
import com.example.aquanliangblogscan.service.ScanBlogServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author guochaobin
 * @date 2022/9/7
 */
@Component
@Slf4j
public class AsyncScanBlogUtil {
    /**
     * *
     *
     * @param i    当前爬虫页
     * @param url  爬虫的url
     * @param page 最后一页的页码
     * @return
     */
    @Async
    public Map<String, Object> getDataAndGenerateOnePage(Integer i, String url, Integer page) {
        Document document2 = null;
        try {
            document2 = Jsoup.parse(new URL(url), 10000);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<Element> spans2 = getSpans(document2);
        List<Article> list = new ArrayList<>(10);
        spans2.forEach(span -> {
            Elements img = span.getElementsByTag("img");
            Article article = new Article();
            if (img.hasAttr("src")) {
                String src = img.attr("src");
                log.debug("\n ---url \n {} \n ---url", src);
                article.setPicUrl(src);
            }
            Elements as = span.getElementsByTag("a");
            if (as.hasText()) {
                Element a = as.get(1);
                if (a != null) {
                    String title = a.children().text();
                    article.setTitle(title);
                    log.debug("\ntitle---\n {} \n ---title", title);
                    Element div = a.parent();
                    Elements divChildren = div.getElementsByTag("div");
                    String content = divChildren.get(2).text();
                    article.setSimpleInt(content);
                    log.debug("\ncontent --\n {} \n content----", content);
                    String lanmu = divChildren.get(4).text();
                    article.setProgram(lanmu);
                    log.debug("\n栏目---\n {} \n ---栏目", lanmu);
                    String publishTime = divChildren.get(5).text();
                    log.debug("\n发布时间 ---\n {} \n", publishTime);
                    article.setPubDate(publishTime);
                    String count = divChildren.get(10).text();
                    log.debug("\n阅读数 -- \n {} \n 阅读数", content);
                    article.setReadCount(count);
                }
            }
            list.add(article);
        });
        ExportParams exportParams = new ExportParams("博客的数据", "第" + i + "页");
        HashMap<String, Object> exportParamsMap = new HashMap<>();
        exportParamsMap.put("title", exportParams);
        exportParamsMap.put("entity", Article.class);
        exportParamsMap.put("data", list);
        ScanBlogServiceImpl.maps[i - 1] = exportParamsMap;
        ScanBlogServiceImpl.latch.countDown();
        log.info("第{}页完成 进度{} %", i, (page - ScanBlogServiceImpl.latch.getCount()) * 100 / page);
        return exportParamsMap;
    }

    public List<Element> getSpans(Document document) {
        Element main = document.getElementsByTag("main").first();
        List<Element> collect = main.getElementsByTag("span").stream()
                .filter(span -> span.getElementsByTag("img").hasAttr("src")).collect(Collectors.toList());
        return collect;
    }
}
